<template>
  <div class="user-info-container">
    <el-card>
      <div class="user-info-box" id="userInfoBox">
        <!-- 标题 -->
        <h2 class="title">{{ $t('msg.member.memberAdd') }}</h2>
        <!-- 头部 -->
        <div class="header">
          <el-form :model="memberFrom" label-width="auto" style="width: 100%">
            <el-form-item :label="$t('msg.member.cardNumber')">
              <el-input
                v-model="memberFrom.cardNumber"
                :disabled="!memberFrom.customize"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.customize')">
              <el-switch v-model="memberFrom.customize" />
            </el-form-item>
            <el-form-item :label="$t('msg.member.name')">
              <el-input
                v-model="memberFrom.realName"
                :placeholder="$t('msg.member.nameTips')"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.phone')">
              <el-input
                v-model="memberFrom.phone"
                :placeholder="$t('msg.member.phoneTips')"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.sex')">
              <el-radio-group v-model="memberFrom.sex">
                <el-radio :label="1">{{ $t('msg.member.sexMale') }}</el-radio>
                <el-radio :label="2">{{ $t('msg.member.sexFemale') }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('msg.member.birthday')">
              <el-date-picker
                v-model="memberFrom.birthday"
                type="date"
                :placeholder="$t('msg.member.birthdayTips')"
                style="width: 100%"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.classifyName')">
              <el-select v-model="memberFrom.classifyId">
                <el-option
                  v-for="(item, index) in classifyList"
                  :key="index"
                  :label="item.classifyName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('msg.member.consumePassword')">
              <el-input
                v-model="memberFrom.consumePassword"
                :placeholder="$t('msg.member.consumePasswordTips')"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.confirmPassword')">
              <el-input
                v-model="memberFrom.confirmPassword"
                :placeholder="$t('msg.member.confirmPasswordTips')"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.idCard')">
              <el-input
                v-model="memberFrom.idCard"
                :placeholder="$t('msg.member.idCardTips')"
              />
            </el-form-item>
            <el-form-item :label="$t('msg.member.remark')">
              <el-input v-model="memberFrom.remark" type="textarea" />
            </el-form-item>
            <el-form-item class="btns">
              <el-button
                type="primary"
                :loading="loading"
                @click="handlerCreate"
                >{{ $t('msg.btn.submit') }}</el-button
              >
              <el-button>{{ $t('msg.btn.cancel') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 内容 -->
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'
import {
  generateMemberNumber,
  memberClassifyList,
  createMember
} from '@/api/member'

const i18n = useI18n()

const memberFrom = ref({
  cardNumber: '',
  realName: '',
  customize: false,
  sex: 1,
  phone: '',
  classifyId: '',
  consumePassword: '',
  confirmPassword: '',
  idCard: '',
  birthday: '',
  remark: ''
})

const resetForm = () => {
  memberFrom.value = {
    cardNumber: '',
    realName: '',
    customize: false,
    sex: 1,
    phone: '',
    classifyId: '',
    consumePassword: '',
    confirmPassword: '',
    idCard: '',
    birthday: '',
    remark: ''
  }
  getMemberNumber()
}

const getMemberNumber = async () => {
  memberFrom.value.cardNumber = await generateMemberNumber()
}
const classifyList = ref([])
const getClassifyList = async () => {
  classifyList.value = await memberClassifyList()
}
getClassifyList()
getMemberNumber()
const loading = ref(false)
const router = useRouter()
const handlerCreate = () => {
  loading.value = true
  createMember(memberFrom.value)
    .then(() => {
      loading.value = false
      // 登录后操作
      resetForm()
      ElMessage.success(i18n.t('msg.member.addMemberSuccess'))
      router.push('/member/list')
    })
    .catch((err) => {
      console.log(err)
      loading.value = false
    })
}
</script>

<style lang="scss" scoped>
.user-info-box {
  width: 600px;
  margin: 0 auto;
  .title {
    text-align: center;
    margin-bottom: 40px;
  }
  .header {
    display: flex;
    ::v-deep .el-descriptions {
      flex-grow: 1;
    }
    .btns {
      ::v-deep .el-form-item__content {
        display: block;
        text-align: center;
      }
    }
  }
}
</style>
